package com.xy.tianyu.module.novel.adapter

import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.BusUtils.post
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.rxlife.coroutine.RxLifeScope
import com.xy.tianyu.R
import com.xy.tianyu.constant.Constant
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendMoreBean
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendMultipleItem
import com.xy.tianyu.module.novel.bean.X
import com.xy.tianyu.module.splash.bean.Vertical
import com.xy.tianyu.module.video.bean.VideoPlayLinkBean
import com.xy.tianyu.network.Api
import com.xy.tianyu.network.ApiParams
import com.xy.tianyu.utils.GlideUtils
import com.xy.tianyu.utils.UniqueUtils
import com.xy.tianyu.view.GridSpacingItemDecoration
import com.xy.tianyu.view.RecycleViewDivider
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.item_competitive_recommend_child_vertical.view.*
import kotlinx.android.synthetic.main.item_competitive_recommend_content.view.*
import kotlinx.android.synthetic.main.item_competitive_recommend_top.view.*
import kotlinx.coroutines.cancel
import rxhttp.RxHttp
import rxhttp.toClass

/**
 * @time 2020/11/5 10:28
 * @class 描述:精品 - 推荐多布局适配器
 */
class CompetitiveRecommendMultipleAdapter(data:MutableList<CompetitiveRecommendMultipleItem>) :BaseMultiItemQuickAdapter<CompetitiveRecommendMultipleItem,BaseViewHolder>(data) {
    private var isLoadMore = false
    private var list = mutableListOf<X>()
    private var isFirstLoad = true
    init {
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_BANNER, R.layout.item_competitive_recommend_top)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_HIGH_SCORE,R.layout.item_competitive_recommend_content)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_PUBLISHED_MASTERPIECES,R.layout.item_competitive_recommend_content)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_MAN_MASTERPIECES,R.layout.item_competitive_recommend_content)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_WOMAN_MASTERPIECES,R.layout.item_competitive_recommend_content)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_FAREWELL_TO_THE_BOOK_SHORTAGE,R.layout.item_competitive_recommend_content)
        addItemType(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_GUESS_YOU_LOVE_WATCHING,R.layout.item_competitive_recommend_content)
    }
    override fun convert(holder: BaseViewHolder, item: CompetitiveRecommendMultipleItem) {
        val data = item.data
        when(item.itemType) {
            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_BANNER -> {
                with(holder.itemView.banner) {
                    adapter = CompetitiveRecommendBannerAdapter(data.list)
                    indicator = CircleIndicator(context)
                    start()
                }
            }
            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_HIGH_SCORE, CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_MAN_MASTERPIECES,
            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_WOMAN_MASTERPIECES -> {
                val verAdapter = CompetitiveRecommendChildVerAdapter(
                    R.layout.item_competitive_recommend_child_vertical,
                    data.list
                )
                with(holder.itemView) {
                    tv_look_at_it_title.text = data.name
                    tv_look_at_it_more.text = data.actionName
                    ry_look_at_it.layoutManager = LinearLayoutManager(context)
                    ry_look_at_it.addItemDecoration(
                        RecycleViewDivider(
                            context, LinearLayoutManager.VERTICAL, 2,
                            ContextCompat.getColor(context, R.color.app_color_bg_EEEEEE)
                        )
                    );
                    ry_look_at_it.adapter = verAdapter
                    view_divide.visibility = View.GONE

                }
            }
            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_PUBLISHED_MASTERPIECES,
            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_FAREWELL_TO_THE_BOOK_SHORTAGE -> {
                val list = mutableListOf<X>()
                val horAdapter = CompetitiveRecommendChildHorAdapter(
                    R.layout.item_competitive_recommend_child_hor,
                    list
                )
                if (item.itemType == CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_PUBLISHED_MASTERPIECES) {
                    list.addAll(data.list.subList(1, 5))
                    val bean = data.list[0]
                    val headView = LayoutInflater.from(context)
                        .inflate(R.layout.item_competitive_recommend_child_vertical, null, false)
                    horAdapter.addHeaderView(headView)

                    with(headView) {
                        val layoutParams = iv_novel_cover_ver.layoutParams
                        layoutParams.width =
                            (ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30f)) / 4
                        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
                        iv_novel_cover_ver.layoutParams = layoutParams
                        Glide.with(context).load(bean.cover).apply(GlideUtils.getRequestOptions())
                            .into(iv_novel_cover_ver)
                        tv_novel_title_ver.text = bean.newBookName
                        tv_novel_des_intro.text = bean.intro
                        tv_novel_author_ver.text = bean.authorName
                        tv_novel_score_ver.text = bean.grade
                        if (bean.bookStatue == "01") {
                            //连载
                            tv_novel_status_ver.text =
                                context.getString(R.string.ty_book_status_one)
                            tv_novel_status_ver.setTextColor(
                                ContextCompat.getColor(
                                    context,
                                    R.color.app_color_FFA428
                                )
                            )
                        } else {
                            //完结
                            tv_novel_status_ver.text =
                                context.getString(R.string.ty_book_status_two)
                            tv_novel_status_ver.setTextColor(
                                ContextCompat.getColor(
                                    context,
                                    R.color.blueColor
                                )
                            )
                        }
                        tv_novel_text_num_ver.text = bean.wordCount
                    }
                } else {
                    list.addAll(data.list)
                }
                with(holder.itemView) {
                    tv_look_at_it_title.text = data.name
                    tv_look_at_it_more.text = data.actionName
                    ry_look_at_it.layoutManager =
                        StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)
                    ry_look_at_it.adapter = horAdapter
                    horAdapter.notifyDataSetChanged()
                }
            }

            CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_GUESS_YOU_LOVE_WATCHING -> {
                if (isFirstLoad){
                    list.addAll(data.list)
                    isFirstLoad = false
                }
                val verAdapter = CompetitiveRecommendChildVerAdapter(R.layout.item_competitive_recommend_child_vertical, list)
                with(holder.itemView) {
                    tv_look_at_it_title.text = data.name
                    tv_look_at_it_more.text = data.actionName
                    ry_look_at_it.layoutManager = LinearLayoutManager(context)
                    ry_look_at_it.addItemDecoration(
                        RecycleViewDivider(
                            context, LinearLayoutManager.VERTICAL, 2,
                            ContextCompat.getColor(context, R.color.app_color_bg_EEEEEE)
                        )
                    )
                    ry_look_at_it.adapter = verAdapter
                    view_divide.visibility = View.GONE
                }

                if (isLoadMore){
                    isLoadMore = false
                    RxLifeScope().launch({
                        val competitiveRecommendMoreBean = getMore()
                        val xList = competitiveRecommendMoreBean.data
                        list.addAll(xList!!)
                        verAdapter.notifyDataSetChanged()
                        cancel()
                    },{},{},{})

                }
            }
        }

    }

    fun loadMore(){
        isLoadMore = true
        notifyDataSetChanged()
    }
    /**
     * 获取更多
     */
    private suspend fun getMore():CompetitiveRecommendMoreBean{
        return RxHttp.postForm(Api.AI_KAN_SHU_FREE_NOVEL_BASE_URL+Api.AI_KAN_SHU_FREE_NOVEL_COMPETITIVE_MORE_URL)
            .addAllHeader(ApiParams.getNovelHeaders())
            .add("uid",UniqueUtils.getNovelUid())
            .add("version",Constant.NOVEL_VERSION)
            .add("cnid",Constant.NOVEL_CNID)
            .add("index",17)
            .toClass<CompetitiveRecommendMoreBean>()
            .await()
    }
}